<template>
  <a-form class="smart-query-form" ref="queryFormRef">
    <a-row class="smart-query-form-row">
      <a-form-item label="产品名称" class="smart-query-form-item">
        <a-input style="width: 300px" v-model:value="queryForm.productName" placeholder="产品名称" />
      </a-form-item>

      <a-form-item class="smart-query-form-item smart-margin-left10">
        <a-button-group>
          <a-button type="primary" @click="onSearch">
            <template #icon>
              <SearchOutlined />
            </template>
            查询
          </a-button>
          <a-button @click="resetQuery">
            <template #icon>
              <ReloadOutlined />
            </template>
            重置
          </a-button>
        </a-button-group>
      </a-form-item>
    </a-row>
  </a-form>

  <a-card size="small" :bordered="false">
    <a-row class="row-margin" ref="tableOperatorRef">
      <a-col :span="18">
        <a-row justify="start">
          <a-button class="btn" @click="showForm" type="primary">新增产品</a-button>
        </a-row>
      </a-col>
      <a-col :span="6">
        <a-row justify="end">
          <TableOperator class="smart-margin-bottom5" v-model="columns" :tableId="TABLE_ID_CONST.UPSTREAM.PRODUCT" :refresh="ajaxQuery" />
        </a-row>
      </a-col>
    </a-row>
    <a-table
      size="small"
      :dataSource="tableData"
      :columns="columns"
      bordered
      rowKey="id"
      :pagination="false"
      :loading="tableLoading"
      :scroll="{ y: scrollY }"
    >
      <template #bodyCell="{ text, record, column }">
        <template v-if="column.dataIndex === 'operate'">
          <div class="smart-table-operate">
            <a-button @click="showForm(record)" type="link">编辑</a-button>
          </div>
        </template>
      </template>
    </a-table>

    <div class="smart-query-table-page">
      <a-pagination
        showSizeChanger
        showQuickJumper
        show-less-items
        :pageSizeOptions="PAGE_SIZE_OPTIONS"
        :defaultPageSize="queryForm.pageSize"
        v-model:current="queryForm.pageNum"
        v-model:pageSize="queryForm.pageSize"
        :total="total"
        @change="ajaxQuery"
        @showSizeChange="ajaxQuery"
        :show-total="(total) => `共${total}条`"
      />
    </div>

    <ProductForm ref="formRef" @reloadList="ajaxQuery" />
    
  </a-card>
</template>
<script setup>
  import { onMounted, onUnmounted, reactive, ref } from 'vue';
  import _ from 'lodash';
  import { PAGE_SIZE_OPTIONS } from '/@/constants/common-const';
  import { productApi } from '/@/api/upstream/product/product-api';
  import { smartSentry } from '/@/lib/smart-sentry';
  import TableOperator from '/@/components/support/table-operator/index.vue';
  import { TABLE_ID_CONST } from '/@/constants/support/table-id-const';
  import { calcTableHeight } from '/@/lib/table-auto-height';
  import { message } from 'ant-design-vue';
  import ProductForm from './components/product-form.vue';

  const columns = ref([
    {
      title: '产品ID',
      dataIndex: 'productId',
      width: 100,
    },
    {
      title: '产品名称',
      dataIndex: 'productName',
      width: 260,
    },
    {
      title: '联通价格',
      dataIndex: 'cuccPrice',
      width: 120,
    },
    {
      title: '电信价格',
      dataIndex: 'ctccPrice',
      width: 120,
    },
    {
      title: '移动价格',
      dataIndex: 'cmccPrice',
      width: 120,
    },
    {
      title: '企业',
      dataIndex: 'enterpriseName',
      ellipsis: true,
      width: 200,
    },
    {
      title: '创建时间',
      dataIndex: 'createTime',
      width: 200,
    },
    {
      title: '更新时间',
      dataIndex: 'updateTime',
      width: 200,
    },
    {
      title: '操作',
      dataIndex: 'operate',
    },
  ]);

  const queryFormState = {
    productName: undefined,
    pageNum: 1,
    pageSize: 10,
  };
  const queryForm = reactive({ ...queryFormState });
  const createDateRange = ref([]);

  const tableLoading = ref(false);
  const tableData = ref([]);
  const total = ref(0);

  function resetQuery() {
    Object.assign(queryForm, queryFormState);
    createDateRange.value = [];
    ajaxQuery();
  }

  function onSearch() {
    queryForm.pageNum = 1;
    ajaxQuery();
  }

  async function ajaxQuery() {
    try {
      tableLoading.value = true;
      let responseModel = await productApi.queryList(queryForm);

      const list = responseModel.data.list;
      total.value = responseModel.data.total;
      tableData.value = list;
    } catch (e) {
      smartSentry.captureError(e);
    } finally {
      tableLoading.value = false;
    }
  }

  // ---------------------------- 添加/修改 ----------------------------
  const formRef = ref();

  function showForm(data) {
    let params = {};
    if (data) {
      params = _.cloneDeep(data);
    }
    formRef.value.show(params);
  }

  // ----------------- 表格自适应高度 --------------------
  const scrollY = ref(100);
  const tableOperatorRef = ref();
  const queryFormRef = ref();

  function autoCalcTableHeight() {
    calcTableHeight(scrollY, [tableOperatorRef, queryFormRef], 30);
  }

  window.addEventListener('resize', autoCalcTableHeight);

  onMounted(() => {
    ajaxQuery();
    autoCalcTableHeight();
  });

  onUnmounted(() => {
    window.removeEventListener('resize', autoCalcTableHeight);
  });
</script>
<style scoped lang="less">
.row-margin {
  margin: 10px 0;

  .btn {
    margin-right: 8px;
  }
}
</style>